package com.hopes.bizz.risk.transfer.handler;

import com.hopes.bizz.common.core.util.StringUtils;
import com.hopes.bizz.risk.common.constant.enums.RiskModDictEnum;
import com.hopes.bizz.risk.control.entity.RiskControlMeasureEntity;
import com.hopes.bizz.risk.control.entity.RiskMeasureTaskRecordEntity;
import com.hopes.bizz.risk.control.service.RiskControlMeasureService;
import com.hopes.bizz.risk.control.service.RiskMeasureTaskRecordService;
import com.hopes.bizz.risk.hid.entity.RiskHidHiddenDangerEntity;
import com.hopes.bizz.risk.hid.service.RiskHidHiddenDangerService;
import com.hopes.bizz.risk.transfer.bean.bo.RiskDataBo;
import com.hopes.bizz.risk.transfer.bean.dto.RiskDangerInvestigationDto;
import com.hopes.bizz.risk.transfer.constant.RiskErrorType;
import com.hopes.bizz.risk.transfer.constant.RiskRequestType;
import com.hopes.bizz.risk.transfer.converter.RiskDangerInvestigationConverter;
import com.hopes.boss.admin.api.resolver.DictResolver;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Objects;

/**
 * @author lyb
 * @version 1.0
 * @date 2024/2/4 16:14
 */

@Service
@Slf4j
public class RiskDangerInvestigationHandler extends AbstractRiskData<RiskDangerInvestigationDto> {

    @Resource
    private RiskMeasureTaskRecordService riskMeasureTaskRecordService;

    @Resource
    private RiskControlMeasureService riskControlMeasureService;

    @Resource
    private RiskHidHiddenDangerService riskHidHiddenDangerService;

    @Override
    protected void saveRiskData(RiskDangerInvestigationDto riskDangerInvestigationDto, RiskDataBo riskDataBo) {
        // 校验危险源是否存在
        String riskObjectId = DictResolver.getItemValueByLabel(RiskModDictEnum.risk_object_code, riskDangerInvestigationDto.getHazardCode());
        if (StringUtils.isBlank(riskObjectId)) {
            saveErrorLog(riskDangerInvestigationDto, riskDataBo, RiskErrorType.HAZ_SOURCE_NOT_FOUND);
            return;
        }
        // 保存
        RiskHidHiddenDangerEntity hiddenDangerEntity = RiskDangerInvestigationConverter.INSTANCE.toEntity(riskDangerInvestigationDto);
        // 关联管控措施
        String riskMeasureId = riskDangerInvestigationDto.getRiskMeasureId();
        if (StringUtils.isNotBlank(riskMeasureId)) {
            RiskControlMeasureEntity riskControlMeasure = riskControlMeasureService.getByUid(riskMeasureId);
            if (Objects.nonNull(riskControlMeasure)) {
                hiddenDangerEntity.setRiskControlId(riskControlMeasure.getId());
                hiddenDangerEntity.setRiskUnitId(riskControlMeasure.getRiskUnitId());
            }
        }
        // 关联排查记录
        String taskRecordUid = riskDangerInvestigationDto.getCheckRecordId();
        if (StringUtils.isNotBlank(taskRecordUid)) {
            RiskMeasureTaskRecordEntity taskRecord = riskMeasureTaskRecordService.lambdaQuery()
                    .eq(RiskMeasureTaskRecordEntity::getUid, taskRecordUid)
                    .one();
            if (Objects.nonNull(taskRecord)) {
                hiddenDangerEntity.setCheckRecordId(taskRecord.getId());
            }
        }
        // 判断是否更新
        String uid = riskDangerInvestigationDto.getId();
        RiskHidHiddenDangerEntity hiddenDanger = riskHidHiddenDangerService.getByUid(uid);
        hiddenDangerEntity.setOrgId(riskDataBo.getOrgId());
        if (Objects.isNull(hiddenDanger)) {
            riskHidHiddenDangerService.save(hiddenDangerEntity);
        } else {
            hiddenDangerEntity.setId(hiddenDanger.getId());
            riskHidHiddenDangerService.updateById(hiddenDangerEntity);
        }
    }

    @Override
    public RiskRequestType getRequestType() {
        return RiskRequestType.RISK_DANGER_INVESTIGATION;
    }
}
